package com.globefrog.framework.log;

import com.globefrog.framework.common.enums.ActionResultEnum;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 活动日志.
 *
 * @author caijinbang
 * @date 2020-11-04 22:48
 */
public class ActionLog {

  /**
   * 请求日期.
   */
  private Date requestDate;
  /**
   * 请求id.
   */
  private String requestId;
  /**
   * 请求动作.
   */
  private String action;
  /**
   * 动作结果.
   */
  private ActionResultEnum result;
  /**
   * 异常.
   */
  private String exception;
  /**
   * 错误信息.
   */
  private String errorMessage;
  /**
   * 追踪日志路径.
   */
  private String traceLogPath;
  /**
   * 耗时时长.
   */
  private long elapsedTime;
  /**
   * 客户端ip.
   */
  private String clientIp;
  /**
   * 请求URI.
   */
  private String requestUri;
  /**
   * http请求方法.
   */
  private String httpMethod;
  /**
   * http状态编码.
   */
  private Integer httpStatusCode;
  /**
   * 上下文.
   */
  private Map<String, String> context;

  public ActionLog() {
    this.result = ActionResultEnum.SUCCESS;
  }

  /**
   * 设置上下文数据.
   *
   * @param key   key值
   * @param value value值
   */
  public void logContext(String key, String value) {
    if (this.context == null) {
      this.context = new ConcurrentHashMap<>();
    }
    this.context.put(key, value);
  }

  long getElapsedTime() {
    return this.elapsedTime == 0L ? System.currentTimeMillis() - this.requestDate.getTime()
        : this.elapsedTime;
  }


  public Date getRequestDate() {
    if (this.requestDate == null) {
      this.requestDate = new Date();
    }
    return (Date) requestDate.clone();
  }

  public void setRequestDate(Date requestDate) {
    this.requestDate = (Date) requestDate.clone();
  }

  public String getRequestId() {
    return requestId;
  }

  public void setRequestId(String requestId) {
    this.requestId = requestId;
  }

  public String getAction() {
    return action;
  }

  public void setAction(String action) {
    this.action = action;
  }

  public ActionResultEnum getResult() {
    return result;
  }

  public void setResult(ActionResultEnum result) {
    this.result = result;
  }

  public String getException() {
    return exception;
  }

  public void setException(String exception) {
    this.exception = exception;
  }

  public String getErrorMessage() {
    return errorMessage;
  }

  public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
  }

  public String getTraceLogPath() {
    return traceLogPath;
  }

  public void setTraceLogPath(String traceLogPath) {
    this.traceLogPath = traceLogPath;
  }

  public void setElapsedTime(long elapsedTime) {
    this.elapsedTime = elapsedTime;
  }

  public String getClientIp() {
    return clientIp;
  }

  public void setClientIp(String clientIp) {
    this.clientIp = clientIp;
  }

  public String getRequestUri() {
    return requestUri;
  }

  public void setRequestUri(String requestUri) {
    this.requestUri = requestUri;
  }

  public String getHttpMethod() {
    return httpMethod;
  }

  public void setHttpMethod(String httpMethod) {
    this.httpMethod = httpMethod;
  }

  public Integer getHttpStatusCode() {
    return httpStatusCode;
  }

  public void setHttpStatusCode(Integer httpStatusCode) {
    this.httpStatusCode = httpStatusCode;
  }

  public Map<String, String> getContext() {
    return context;
  }

  public void setContext(Map<String, String> context) {
    this.context = context;
  }
}
